// 设置文件
import setting from '@/setting.js'

export default {
  namespaced: true,
  state: {
    // 顶栏菜单
    header: [],
    // 侧栏菜单
    aside: [],
    // 侧边栏收缩
    asideCollapse: setting.menu.asideCollapse
  },
  actions: {
    /**
     * 设置侧边栏展开或者收缩
     * @param {Object} state vuex state
     * @param {Boolean} collapse is collapse
     */
    asideCollapseSet ({ state, dispatch }, collapse) {
      return new Promise(async resolve => {
        // store 赋值
        state.asideCollapse = collapse
        // 持久化
        await dispatch('d2admin/db/set', {
          dbName: 'sys',
          path: 'menu.asideCollapse',
          value: state.asideCollapse,
          user: true
        }, { root: true })
        // end
        resolve()
      })
    },
    /**
     * 切换侧边栏展开和收缩
     * @param {Object} state vuex state
     */
    asideCollapseToggle ({ state, dispatch }) {
      return new Promise(async resolve => {
        // store 赋值
        state.asideCollapse = !state.asideCollapse
        // 持久化
        await dispatch('d2admin/db/set', {
          dbName: 'sys',
          path: 'menu.asideCollapse',
          value: state.asideCollapse,
          user: true
        }, { root: true })
        // end
        resolve()
      })
    },
    /**
     * 从持久化数据读取侧边栏展开或者收缩
     * @param {Object} state vuex state
     */
    asideCollapseLoad ({ state, dispatch }) {
      return new Promise(async resolve => {
        // store 赋值
        state.asideCollapse = await dispatch('d2admin/db/get', {
          dbName: 'sys',
          path: 'menu.asideCollapse',
          defaultValue: setting.menu.asideCollapse,
          user: true
        }, { root: true })
        // end
        resolve()
      })
    }
  },
  mutations: {
    /**
     * @description 设置顶栏菜单
     * @param {Object} state vuex state
     * @param {Array} menu menu setting
     */
    headerSet (state, menu) {
      // store 赋值
      state.header = menu
    },
    /**
     * @description 设置侧边栏菜单
     * @param {Object} state vuex state
     * @param {Array} menu menu setting
     */
    asideSet (state, menu) {
      // store 赋值
      state.aside = menu
	  
	  //暂时使用的代码，过后删除
	  // state.aside = [
		 //  {
		 //  	id: 1000,
		 //  	type: 1,
		 //  	parentId: -1,
		 //  	title: '模板',
		 //  	icon: "anchor",
		 //  	children: [
		 //  		{
		 //  			id: 1001,
		 //  			type: 1,
		 //  			parentId: 1000,
		 //  			path: "/sample/list",
		 //  			title: '普通列表'
		 //  		},
			// 	{
			// 		id: 1002,
			// 		type: 1,
			// 		parentId: 1000,
			// 		path: "/sample/add",
			// 		title: '添加表单'
			// 	},
			// 	{
			// 		id: 1002,
			// 		type: 1,
			// 		parentId: 1000,
			// 		path: "/sample/add2",
			// 		title: '添加表单(带表格)'
			// 	}
		 //  	]
		 //  }
	  // ];
	  // state.aside = state.aside.concat(menu);
    }
  }
}
